package cn.zzp.org.controller;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.util.ExcelUtils;
import cn.zzp.basic.util.JsonResult;
import cn.zzp.basic.util.PageList;
import cn.zzp.org.domain.Shop;
import cn.zzp.org.domain.ShopDto;
import cn.zzp.org.domain.ShopLog;
import cn.zzp.org.query.ShopQuery;
import cn.zzp.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName ShopController
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/17 10:24
 */
@Api(tags = "店铺模块")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Shop> queryAll() {
        return shopService.queryAll();
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name="id", dataType="Long", required=true, value="用户Id")
    })
    @ApiOperation(value = "根据id查询一条数据" )
    @GetMapping("/{id}")
    public Shop queryOne(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    @ApiOperation(value = "添加或修改" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Shop employee){
        try {
            if(employee.getId() == null){
                shopService.add(employee);
            }else{
                shopService.update(employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页高级查询" )
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        try {
            return shopService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            shopService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 提交店铺注册信息
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me("验证失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me("系统错误，请联系管理员");
        }
    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PutMapping("/audit/pass")
    public JsonResult pass(@RequestBody ShopLog log){
        try {
            shopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me("操作失败");
        }
    }

    /**
     * 驳回
     * @param log
     * @return
     */
    @PutMapping("/audit/reject")
    public JsonResult reject(@RequestBody ShopLog log){
        try {
            shopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me("操作失败");
        }
    }

    /**
     * 数据导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 数据导入
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){


        try {
            List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);

            shops.forEach(System.out::println);

            shopService.patchAdd(shops);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        shopDtos.forEach(System.out::println);
        return shopDtos;
    }
}
